import junit.framework.TestCase;


public class DriverAssignmentTest extends TestCase {
	public void testDeliveryTime(){
		Delivery delivery = createDelivery("D01", "Addrs1", "0100", "0100");
		assertEquals(delivery.getDesiredDeliveryTimeInMinutes(), 60);

		delivery = createDelivery("D01", "Addrs1", "0012", "0100");
		assertEquals(delivery.getDesiredDeliveryTimeInMinutes(), 12);

		delivery = createDelivery("D01", "Addrs1", "0a12", "0100");
		assertEquals(delivery.getDesiredDeliveryTimeInMinutes(), 0);

		delivery = createDelivery("D01", "Addrs1", "abcd", "0100");
		assertEquals(delivery.getDesiredDeliveryTimeInMinutes(), 0);
		
		delivery = createDelivery("D01", "Addrs1", "0123456", "0100");
		assertEquals(delivery.getDesiredDeliveryTimeInMinutes(), 60+23);
	}
	
	public void testDeliveryDuration(){
		Delivery delivery = createDelivery("D01", "Addrs1", "0100", "0100");
		assertEquals(delivery.getEstimatedDeliveryDurationInMinutes(), 60);

		delivery = createDelivery("D01", "Addrs1", "0100", "0012");
		assertEquals(delivery.getEstimatedDeliveryDurationInMinutes(), 12);

		delivery = createDelivery("D01", "Addrs1", "0100", "0a12");
		assertEquals(delivery.getEstimatedDeliveryDurationInMinutes(), 0);

		delivery = createDelivery("D01", "Addrs1", "0100", "abcd");
		assertEquals(delivery.getEstimatedDeliveryDurationInMinutes(), 0);
		
		delivery = createDelivery("D01", "Addrs1", "0100", "0123456");
		assertEquals(delivery.getEstimatedDeliveryDurationInMinutes(), 60+23);
	}

	public void testConflitHoraire(){
		Delivery d1 = createDelivery("D01", "Addrs1", "0100", "0100");
		Delivery d2 = createDelivery("D02", "Addrs2", "0100", "0100");
		Delivery d3 = createDelivery("D03", "Addrs3", "0230", "0100");
		Delivery d4 = createDelivery("D04", "Addrs4", "0330", "0100");

		assertEquals(AssignmentValidator.isConflitHoraire(d1, d2), true);
		assertEquals(AssignmentValidator.isConflitHoraire(d1, d3), true); //Oui car il sera sur son retour
		assertEquals(AssignmentValidator.isConflitHoraire(d1, d4), false); //Non, il peut prendre les deux sans probleme
	}

	public void testTooManyHoursSenior(){
		Driver driver = createDriver("DRV001", "Francois", "CCC", "SNR");
		Delivery d1 = createDelivery("D01", "Addrs1", "0100", "0200");
		Delivery d2 = createDelivery("D02", "Addrs2", "0100", "0200");
		Delivery d3 = createDelivery("D03", "Addrs3", "0530", "0210");
		Delivery d4 = createDelivery("D04", "Addrs4", "0530", "0200");
		
		AssignmentValidator av = new AssignmentValidator(driver, d1);
		assertEquals(av.isValidAssignment(), true);
		driver.assignDelivery(d1);

		av = new AssignmentValidator(driver, d2);
		assertEquals(av.isValidAssignment(), false);
		assertEquals(av.isConflitHoraire(), true);
		assertEquals(av.isTooManyHours(), false);
		
		av = new AssignmentValidator(driver, d3);
		assertEquals(av.isValidAssignment(), false);
		assertEquals(av.isConflitHoraire(), false);
		assertEquals(av.isTooManyHours(), true);	
		
		av = new AssignmentValidator(driver, d4);
		assertEquals(av.isValidAssignment(), true);
		driver.assignDelivery(d4);
	}

	public void testTooManyHoursJunior(){
		Driver driver = createDriver("DRV001", "Francois", "CCC", "JNR");
		Delivery d1 = createDelivery("D01", "Addrs1", "0100", "0200");
		Delivery d2 = createDelivery("D02", "Addrs2", "0100", "0200");
		Delivery d3 = createDelivery("D03", "Addrs3", "0530", "0210");
		Delivery d4 = createDelivery("D04", "Addrs4", "1130", "0200");
		
		AssignmentValidator av = new AssignmentValidator(driver, d1);
		assertEquals(av.isValidAssignment(), true);
		driver.assignDelivery(d1);

		av = new AssignmentValidator(driver, d2);
		assertEquals(av.isValidAssignment(), false);
		assertEquals(av.isConflitHoraire(), true);
		assertEquals(av.isTooManyHours(), false);
		
		av = new AssignmentValidator(driver, d3);
		assertEquals(av.isValidAssignment(), true);
		driver.assignDelivery(d3);	
		
		av = new AssignmentValidator(driver, d4);
		assertEquals(av.isValidAssignment(), false);
		assertEquals(av.isConflitHoraire(), false);
		assertEquals(av.isTooManyHours(), true);	
	}
	
	//Create Methods
	private Driver createDriver(String id, String firstName, String lastName, String type){
		Driver driver = new Driver();
		driver.setDriverID(id);
		driver.setFirstName(firstName);
		driver.setLastName(lastName);
		driver.setType(type);
		return driver;
	}
	
	private Delivery createDelivery(String id, String address, String desiredDeliveryTime, String duration){
		Delivery delivery = new Delivery();
		delivery.setDeliveryID(id);
		delivery.setAddress(address);
		delivery.setDesiredDeliveryTime(desiredDeliveryTime);
		delivery.setEstimatedDeliveryDuration(duration);
		return delivery;
	}
}
